package com.itheima.appserver.controller;

import com.itheima.appserver.service.MessagesService;
import com.itheima.model.enums.CommentType;
import com.itheima.model.vo.PageResult;
import com.itheima.model.vo.UserInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/messages")
public class MessagesController {
    @Autowired
    private MessagesService messagesService;

    //查询环信用户
    @GetMapping("/userinfo")
    public ResponseEntity findHuanXinuser(String hunaxinId) {
        UserInfoVo vo = messagesService.findHuanXinuser(hunaxinId);
        return ResponseEntity.ok(vo);
    }

    //添加好友
    @PostMapping("/contacts")
    public ResponseEntity contacts(@RequestBody Map map) {
        //获取用户id,前端是inteage,需要转换
        Long friendId = Long.valueOf(map.get("userId").toString());
        //调用service
        messagesService.contacts(friendId);
        return ResponseEntity.ok(null);
    }

    //联系人列表
    @GetMapping("/contacts")
    public ResponseEntity findByFriend(@RequestParam(defaultValue = "1") Integer page,
                                       @RequestParam(defaultValue = "10") Integer pagesize,
                                       String keyword) {
        //调用serivce
        PageResult pr = messagesService.findByFriend(page, pagesize, keyword);

        return ResponseEntity.ok(pr);
    }

    //点赞列表
    @GetMapping("/likes")
    public ResponseEntity likes(@RequestParam(defaultValue = "1") Integer page,
                                @RequestParam(defaultValue = "10") Integer pagesize) {
        //调用service查询
        PageResult pr = messagesService.findUservo(page, pagesize, CommentType.LIKE);

        return ResponseEntity.ok(pr);
    }

    //评论列表
    @GetMapping("/comments")
    public ResponseEntity comments(@RequestParam(defaultValue = "1") Integer page,
                                   @RequestParam(defaultValue = "10") Integer pagesize) {
        //调用service查询
        PageResult pr = messagesService.findUservo(page, pagesize, CommentType.COMMENT);
        return ResponseEntity.ok(pr);
    }

    //喜欢列表
    @GetMapping("/loves")
    public ResponseEntity loves(@RequestParam(defaultValue = "1") Integer page,
                                @RequestParam(defaultValue = "10") Integer pagesize) {
        //调用service查询
        PageResult pr = messagesService.findUservo(page, pagesize, CommentType.LOVE);
        return ResponseEntity.ok(pr);
    }

    //公告列表
    @GetMapping("/announcements")
    public ResponseEntity announcements(@RequestParam(defaultValue = "1") Integer page,
                                        @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pr = messagesService.announcements(page, pagesize);
        return ResponseEntity.ok(pr);
    }
}
